import {Message} from "../../models/message";
import {showToast} from "../../util/ui";

Page({
  data: {
    list: [],
    active: 'all',
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      isHandle: ''
    },
    listTotal: 0,
    lock: false,
    showLoading: true
  },
  async onLoad() {
    await this.initList(true)
  },
  async initList(isEmpty) {
    this.setData({
      showLoading: true,
      lock: true,
      list: isEmpty ? [] : this.data.list
    })
    const res = await Message.doApiGetMyMessageList(this.data.queryParams)
    res.rows.forEach(item => {
      item.isHandle = +item.isHandle
    })
    this.setData({
      listTotal: res.total,
      list: this.data.list.concat(res.rows),
      showLoading: false,
      lock: false
    })
  },
  async onChange(event) {
    const tabName = event.detail.name
    switch (tabName) {
      case 'all':
        this.data.queryParams.isHandle = ''
        break
      case 'untreated':
        this.data.queryParams.isHandle = '0'
        break
      case 'treated':
        this.data.queryParams.isHandle = '1'
        break
    }
    await this.initList(true)
  },
  async onReachBottom() {
    const listLength = this.data.list.length
    if (this.data.listTotal <= listLength) {
      showToast('没有更多数据了')
      return
    }
    this.setData({
      "queryParams.pageNum": this.data.queryParams.pageNum++
    })
    if (this.data.lock) return
    await this.initList(false)
  },
})
